﻿using System;
using System.Collections.Generic;
using System.Data;
using visinia.SmartEngine.Cache;
using visinia.SmartEngine.Modules;
using visinia.SmartEngine.Pages;
using visinia.SmartEngine.Providers;

namespace visinia.SmartEngine.Installer
{
    /// <summary>
    /// ModulesInstalledController class manages the registered installed modules    
    /// </summary>
    public class ModulesInstalledController
    {
        public const string KEY_SITE_MODULES_INSTALLED = "siteInstalledModules";

        public static int RegisterModule(ModuleInstalled module)
        {
            // is this module already registered
            if (GetInstalledModules().Exists(p => (string.Compare(p.Name, module.Name, true) == 0) && (string.Compare(p.Package, module.Package, true) == 0)))
                return 0;
            IDataReader dr = SmartEngineDataProvider.Provider.RegisterModule(module.Name, module.Package, module.ModuleUrl, module.ModuleSettingsUrl, module.Icon, module.Image, module.Help);
            int index = 0;
            while (dr.Read())
            {
                if (dr[0] != Convert.DBNull)
                    index = (int)dr[0];
            }
            dr.Close();
            DataCache.DeleteCacheMultiple(KEY_SITE_MODULES_INSTALLED);
            return index;            
        }

        public static void UnRegisterModule(int ModuleInstalledId)
        {
            // remove all instances of this module from website pages
            DeleteModuleInstances(ModuleInstalledId);
            SmartEngineDataProvider.Provider.UnRegisterModule(ModuleInstalledId);
            DataCache.DeleteCacheMultiple(KEY_SITE_MODULES_INSTALLED);
        }

        public static void DeleteModuleInstances(int InstalledModuleId)
        {
            foreach (PageInfo page in PageController.GetPages())
            {
                List<ModuleInfo> modules = ModuleController.GetModules(page.PageId).FindAll(p => p.ModuleInstalledId == InstalledModuleId);
                foreach (ModuleInfo module in modules)
                    ModuleController.DeleteModule(module.ModuleId);
            }
        }

        /// <summary>
        /// Gets the specified Installed Module from the Data Store
        /// </summary>
        /// <returns>Return the installed Module</returns>
        public static ModuleInstalled GetInstalledModule(int ModuleInstalledId)
        {
            return GetInstalledModules().Find(p => p.ModuleInstalledId == ModuleInstalledId);
        }

        /// <summary>
        /// Gets All Installed Modules from the Data Store
        /// </summary>
        /// <returns>List of the installed Modules</returns>
        public static List<ModuleInstalled> GetInstalledModules()
        {
            List<ModuleInstalled> list = (List<ModuleInstalled>)DataCache.GetCache(KEY_SITE_MODULES_INSTALLED);
            if (list == null)
            {
                list = FillInstalledModules(SmartEngineDataProvider.Provider.GetInstalledModules());
                DataCache.SetCache(KEY_SITE_MODULES_INSTALLED, list, new TimeSpan(0, 20, 0));
            }
            return list;
        }

        #region HELPERS
        private static List<ModuleInstalled> FillInstalledModules(IDataReader dr)
        {
            List<ModuleInstalled> list = new List<ModuleInstalled>();
            while (dr.Read())
            {
                ModuleInstalled module = new ModuleInstalled();
                if (dr["ModuleInstalledId"] != Convert.DBNull)
                    module.ModuleInstalledId = (int)dr["ModuleInstalledId"];
                if (dr["Package"] != Convert.DBNull)
                    module.Package = (string)dr["Package"];
                if (dr["Name"] != Convert.DBNull)
                    module.Name = (string)dr["Name"];
                if (dr["ModuleUrl"] != Convert.DBNull)
                    module.ModuleUrl = (string)dr["ModuleUrl"];
                if (dr["ModuleSettingsUrl"] != Convert.DBNull)
                    module.ModuleSettingsUrl = (string)dr["ModuleSettingsUrl"];
                if (dr["Icon"] != Convert.DBNull)
                    module.Icon = (string)dr["Icon"];
                if (dr["Image"] != Convert.DBNull)
                    module.Image = (string)dr["Image"];
                if (dr["Help"] != Convert.DBNull)
                    module.Help = (string)dr["Help"];
                list.Add(module);
            }
            dr.Close();
            return list;
        }
        #endregion       
    }
}
